var gulp = require("gulp");
var tmodjs = require("gulp-tmod");
var _ = require("lodash");

gulp.task("default", ["watch"], function(){
    console.log("Starting gulp");
});

// watch the change of the template file, event driven
gulp.task("watch", function(){
    return gulp.watch("src/**/tmpl/*.html", function(event){
        var base = __dirname;
        var path = event.path;
        var relatviePath = _.replace(path, base + "\\", "");

        relatviePath = relatviePath.replace(/\\/g, "/"); 
        var matches = relatviePath.match(/(.*\/tmpl)(.*)/);
        var relativeBase = matches[1];

        console.log("Changing ", path, " ", relativeBase);

        gulp.src(relativeBase + "/**/*.html")
            .pipe(tmodjs({
                templateBase: relativeBase
            }))
            .pipe(gulp.dest(relativeBase + "/build"));
    });
});